<?php

namespace app\common\event\site;

use app\admin\service\AdminGroupService;
use app\admin\service\SiteGroupService;
use app\common\exception\AdminApiException;

class UpdateSiteAuthEvent
{


    /**
     * 站点保存后重新刷新站点权限列表
     * @return void
     */
    public function handle($site)
    {
        if(!$site) return;

        $site_group = (new SiteGroupService())->info(['id' => $site['site_group_id']]);
        if(!$site_group) throw new AdminApiException('站点套餐不存在！' );


        $data = ['rules' => is_array($site_group['group_roles']) ? implode(',',$site_group['group_roles']) : $site_group['group_roles']];
        $where = ['site_id' => $site['site_id'],'pid' => 0];

        (new AdminGroupService())->update($data,$where);
        (new AdminGroupService())->updateChildRules(['site_id' => $site['site_id'],'pid' <> 0],$data['rules']);

    }


}